package com.ewhale.feitengguest.presenter.user.mine;

import com.ewhale.feitengguest.api.ApiHelper;
import com.ewhale.feitengguest.dto.AliAuthInfoDto;
import com.ewhale.feitengguest.dto.AuthInfoDto;
import com.ewhale.feitengguest.view.user.mine.UserInfoView;
import com.simga.library.base.IPresenter;
import com.simga.library.http.APIException;

import java.util.HashMap;
import java.util.Map;

public class UserInfoPresenter extends IPresenter {

    public void resetHeadPhoto(String image) {
        mView.showProLoading();
        request(1, ApiHelper.USER_API.resetHeadPhoto("app/account/updateHeadPhoto.json",image), image);
    }


    public void resetName(String name) {
        mView.showProLoading();
        request(2, ApiHelper.USER_API.resetName(name), name);
    }

    public void resetNickName(String name) {
        mView.showProLoading();
        request(3, ApiHelper.USER_API.resetNickName("app/account/updateNickName.json",name), name);
    }

    public void resetSex(int sex) {
        mView.showProLoading();
        request(4, ApiHelper.USER_API.resetSex(sex), sex);
    }


    public void resetBirthday(int year, int mouth) {
        mView.showProLoading();
        request(5, ApiHelper.USER_API.resetBirthday(year,mouth), year+"-"+mouth);
    }


    public void bindThird(String openid, int type) {
        mView.showProLoading();
        Map<String,Object> params = new HashMap<>();
        params.put("openid",openid);
        params.put("type",type);
        request(6, ApiHelper.USER_API.bindThree(openid,type), params);
    }

    public void getAliAuthInfo() {
        mView.showProLoading();
        request(7, ApiHelper.USER_API.getAliInfo(), null);
    }

    public void loadAuthInfo() {
        mView.showProLoading();
        request(8, ApiHelper.USER_API.getAuthInfo(), null);
    }

    @Override
    public <T> void onNext(int postCode, T result, Object data) {
        super.onNext(postCode, result, data);
        switch (postCode) {
            case 1:
                ((UserInfoView) mView).resetAvatar((String) data);
                break;
            case 2:
                ((UserInfoView) mView).resetName((String) data);
                break;
            case 3:
                ((UserInfoView) mView).resetNickName((String) data);
                break;
            case 4:
                ((UserInfoView) mView).resetSex((int) data);
                break;
            case 5:
                ((UserInfoView) mView).resetBirthday((String) data);
                break;
            case 6:
                ((UserInfoView) mView).bindSuccess((Map<String,Object>) data);
                break;
            case 7:
                ((UserInfoView) mView).handleInfo((AliAuthInfoDto) result);
                break;
            case 8:
                ((UserInfoView) mView).showAuthInfo((AuthInfoDto) result);
                break;
        }
    }

    @Override
    public void otherException(int postCode, Object data, APIException e) {
        super.otherException(postCode, data, e);
        mView.showErrorMessage(postCode, e.getCode(), e.getDisplayMessage());
    }

}
